import { message } from 'antd';
import pageTabUtil from '@/utils/pageTabUtil';
import {
    getRecruitListgw,
    getAddpost,
    getEditpost,
    fetchHide,
    getPostDetail,
    fetchDelect,
    fetchSort
} from '@/services/setting/recruitManage';

export default {
    namespace: 'recruitManage',
    state: {
        recruitList: [],
        pagination: {
            pageSize: 10,
            current: 1,
            total: 0
        },
        postDetail: {}, // 职位表单详情
        orderBy: ''
    },
    effects: {
        // 获取招聘列表
        *fetchRecruitList({ payload }, { put, call }) {
            const result = yield call(getRecruitListgw, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        recruitList: result.data.rows,
                        pagination: {
                            current: result.data.pageNo,
                            pageSize: result.data.pageSize,
                            total: result.data.total
                        }
                    }
                });
            }
        },
        // 设置职位是否隐藏
        *fetchHide({ payload, callback }, { call }) {
            const result = yield call(fetchHide, payload);
            if (callback) callback(result);
        },
        // 删除职位
        *fetchDelect({ payload, callback }, { call }) {
            const result = yield call(fetchDelect, payload);
            if (callback) callback(result);
        },
        // 职位排序
        *fetchSort({ payload, callback }, { call }) {
            const result = yield call(fetchSort, payload);
            if (callback) callback(result);
        },
        // 发布职位提交
        *addPost({ payload }, { call }) {
            const result = yield call(getAddpost, payload);
            if (result.code === 10000) {
                message.success(result.msg);
                // yield put({
                //     type: 'save',
                //     payload: {
                //         postDetail: {}
                //     }
                // });
                // history.push('/office/info/webSite/recruitList');
                pageTabUtil.goBack();
            } else {
                message.error(result.msg);
            }
        },
        // 编辑职位
        *editPost({ payload }, { call }) {
            const result = yield call(getEditpost, payload);
            if (result.code === 10000) {
                message.success(result.msg);
                // history.push('/office/info/webSite/recruitList');
                pageTabUtil.goBack();
            } else {
                message.error(result.msg);
            }
        },
        // 获取职位详情
        *fetchPostDetail({ payload }, { put, call }) {
            const result = yield call(getPostDetail, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        postDetail: result.data
                    }
                });
            }
        }
    },
    reducers: {
        setPagination(state, { pagination }) {
            return {
                ...state,
                pagination
            };
        },
        save(state, { payload }) {
            return {
                ...state,
                ...payload
            };
        }
    },
    subscriptions: {
        setup({ dispatch, history }) {
            const { query } = history.location;
            dispatch({
                type: 'recruitManage/save',
                payload: {
                    orderBy: query.sort
                }
            });
        }
    }
};
